package com.itheima.ribbon.client.web;

import com.itheima.ribbon.client.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 加上＠RestController 注解，开启RestController 的功能
 */
@RestController
public class RibbonController {
    
    @Autowired
    RibbonService ribbonService;
    
    @GetMapping("/hello")
    public String hello(@RequestParam(required = false, defaultValue = "itheima") String name) {
        return ribbonService.hello(name);
    }
    
    @Autowired
    private LoadBalancerClient loadBalancer;
    
    /**
     * 通过LoadBalancerClient 去选择一个nacos的服务实例的信息， 并将该信息返回
     */
    @GetMapping("/testribbon")
    public String testRibbon() {
        ServiceInstance instance = loadBalancer.choose("nacos-client");
        return instance.getHost() + ":" + instance.getPort();
    }
}
